<template>
  <div class="home">

    <van-nav-bar title="首页" left-text="" right-text="" >
      <!-- <template #left>
        <van-icon name="cross" size="19" color="#000" />
      </template> -->
      <!-- <template #right>
        <van-icon name="ellipsis" size="22" color="#000" />
      </template> -->
    </van-nav-bar>
    <Swiper :banner-list="bannerList" />
    <!-- <Diamond :diamond-list="diamondList" /> -->
    <!-- <Goods-vant-card v-model="isLoading" title-name="可抢商品列表" :goods-list="newGoodsList" :is-finished="true"
      @onReachBottom="onReachBottom" /> -->
    <Goods-limit title-name="可抢商品列表" :goods-list="newGoodsList" @child-event="parentMethod" :is-finished="true" />
    <Goods-limit2 title-name="委托待售商品列表" :goods-list="hotGoodsList" @child-event="parentMethod" :is-finished="true" />
    <!-- <Goods v-model="isLoading" title-name="为你推荐" :goods-list="goodsList" :is-finished="isFinished"
      @onReachBottom="onReachBottom" /> -->
    <!-- <back-top />
    <Skeleton v-if="isSkeletonShow" /> -->



  </div>
</template>

<script>
import { getHomeData } from '@/api/user'
//bind_wechat
import { Toast } from 'vant'
import { bind_wechat } from '@/api/user'
import { getGoodslist, gettrustlist } from '@/api/user'
import Header from './modules/Header'
import Swiper from './modules/Swiper'
// import Goods from './modules/Goods'
import Goods from './modules/GoodsVantCard'
import GoodsLimit from './modules/GoodsLimit'
import GoodsLimit2 from './modules/GoodsLimit2'
import GoodsVantCard from './modules/GoodsVantCard'
import Diamond from './modules/Diamond'
// import Session from './modules/Session'
import Skeleton from './modules/Skeleton'

export default {
  name: 'Home',
  components: {
    Header,
    Swiper,
    Diamond,
    // Session,
    Goods,
    GoodsLimit,
    Skeleton,
    GoodsVantCard,
    GoodsLimit2
  },
  data() {
    return {
      showDialog: false,
      mallConfig: {},
      bannerList: [],
      diamondList: [],
      sessionList: [],
      newGoodsList: [],
      hotGoodsList: [],
      goodsList: [],
      pageSize: 6,
      pageNum: 1,
      isLoading: false,
      isFinished: false,
      isSkeletonShow: true
    }
  },
  // created() {
  //   console.log("3333");

  //   this.getHomeIndexData()
  //   this.getRecommonGoodsList()
  //   this.gettrustlist()
  // },
  // 与methods、mounted "同级"
  // 注意: 不要去掉immediate！！
  // 注意: 不要去掉immediate！！
  // 注意: 不要去掉immediate！！
  // 注意: 不要去掉immediate！！
  // watch: {
  //   $route: {
  //     handler(to) {
  //       // 也可以判断路由,从而灵活执行
  //       if (to.name === 'Home') {
  //         this.getHomeIndexData()
  //         this.getRecommonGoodsList()
  //         this.gettrustlist()
  //       }
  //     },
  //     // 是否首次执行(必须加这个,否则无效)
  //     immediate: true
  //   }
  // },

  // watch: {
  //   '$route': function () {
  //     // 重新执行需要执行的代码
  //     this.getHomeIndexData()
  //     this.getRecommonGoodsList()
  //     this.gettrustlist()
  //   }
  // },
  mounted() {
    this.getCode()
    this.getHomeIndexData()
    // this.getRecommonGoodsList()
    // this.gettrustlist()


  },
  methods: {
    getCode() {
      const url = new URL(window.location.href);
      // 获取查询参数
      const queryParams = url.searchParams;
      // 例如，获取名为 'param' 的参数
      const paramValue = queryParams.get('code');
    
      if (paramValue) {
        let obj = {
          code: paramValue
        }
        bind_wechat(obj).then((res) => {
          if (res.code == 1) {
            var currentUrl = window.location.href;
            // 查找"?"在URL中的位置
            var questionMarkIndex = currentUrl.indexOf("?");
            // 如果存在"?"，则去掉它及其后面的所有内容
            if (questionMarkIndex !== -1) {
              // 获取不包含查询参数的URL路径
              var pathWithoutQuery = currentUrl.substring(0, questionMarkIndex);

              // 设置新的URL，去掉查询参数
              // window.location.href = pathWithoutQuery;
              // 重新加载页面
              window.close();
 
              window.location.href = pathWithoutQuery;
            }
          }else{
            Toast(res.msg)
          }
          // Toast(res.msg)
        })
        // 获取当前URL
        // var currentUrl = window.location.href;
        // // 查找"?"在URL中的位置
        // var questionMarkIndex = currentUrl.indexOf("?");
        // // 如果存在"?"，则去掉它及其后面的所有内容
        // if (questionMarkIndex !== -1) {
        //   // 获取不包含查询参数的URL路径
        //   var pathWithoutQuery = currentUrl.substring(0, questionMarkIndex);

        //   // 设置新的URL，去掉查询参数
        //   window.location.href = pathWithoutQuery;
        //   // 重新加载页面
        //   window.location.reload();
        // }
      }
    },
    parentMethod() {
      // this.getRecommonGoodsList()
      // this.gettrustlist()
      this.getHomeIndexData()
    },

    getHomeIndexData() {
      getHomeData().then(res => {
        // const { data } = res
        if (res.code == 1) {
          this.bannerList = res.data.banner
          this.newGoodsList = res.data.goods
          this.hotGoodsList = res.data.trust
        }


      })
    },
    getRecommonGoodsList() {
      let obj = {
        page: 1,
        pageSize: 100
      }
      getGoodslist(obj).then(res => {
        if (res.code == 1) {
          this.newGoodsList = res.data.list
        }

      })
    },
    gettrustlist() {
      let obj = {
        page: 1,
        pageSize: 100
      }
      gettrustlist(obj).then(res => {
        if (res.code == 1) {
          this.hotGoodsList = res.data.list
        }

      })
    },
    // reach-bottom
    // onReachBottom() {
    //   this.pageNum += 1
    //   this.getRecommonGoodsList()
    // }
  }
}
</script>

<style lang="scss" scoped>
.home {
  background: #f5f5f5;
  min-height: 100vh;

  .logo {
    display: block;
    width: 200px;
    margin: 0 auto;
    padding-top: 40px;
  }

  .menu__right {
    width: 50px;
  }
}
</style>
